import { login } from '@/api/mes-web/login-for-reviewparts'
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken(),
  reviewpartsname: ''
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_REVIEWPARTSNAME: (state, reviewpartsname) => {
    state.reviewpartsname = reviewpartsname
  }
}

const actions = {
  // login for keyparts
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      // pageType: '1'区别于关键件扫描
      login({ username: username.trim(), password: password, pageType: '1' }).then(response => {
        // console.log(response)
        if (response.code === 200) {
          commit('SET_TOKEN', 'login-for-reviewparts')
          setToken('login-for-reviewparts')
          commit('SET_REVIEWPARTSNAME', username)
          resolve()
        } else {
          reject()
        } 
      }).catch(error => {
        reject(error)
      })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
